<?php

/**
 * register actions.
 *
 * @package    ufico
 * @subpackage register
 * @author     Ameen SHoman
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */

class cms_profileActions extends sfActions
{

	public function executeChange_password($request)
	{
		append_title('Change password');
		$this->form = new changePasswordForm();

		if ($request->isMethod('post'))
		{
			$this->form->bind($request->getParameter('changepassword'));
			if ($this->form->isValid())
			{
				$password = $request->getParameter('changepassword');
				$sf_guard_user = sfGuardUserPeer::retrieveByPk($this->getUser()->getGuardUser()->getId());
				$sf_guard_user->setPassword($password['newpassword']);
				try
				{
					$sf_guard_user->save();
				}
				catch (PropelException $e)
				{
					$error_message = 'Error! Could not update password.';
					return sfView::ERROR;
				}
				$this->getUser()->setFlash('notice', "Password modification done !");
				$this->redirect('cms_profile/change_password');
			}
		}

	}

	public function executeReq_password(sfWebRequest $request)
	{
		append_title("Request password");
		if($request->isMethod('post')){
			$username = $request->getParameter('username' );
			$guard_user = sfGuardUserPeer::retrieveByUsername($username);
			if($guard_user){
				$new_pass = strtoupper(substr(md5(rand()),0,8));
				$guard_user->setPassword($new_pass);
				$guard_user->save();

				if(!class_exists('nahoMail')){
					$body = get_partial('cms_profile/mails/req_pass' , array('guard_user'=>$guard_user , 'new_pass'=> $new_pass ));


					sfContext::getInstance()->getMailer()->composeAndSend( CMS_globals::get_admin_attr('admin_mail'), $guard_user->getProfile()->getEmail(), __('request password') , $body );

				}else{

					$body = nahoMail::getBody('partial','cms_profile/mails/req_pass' , array('guard_user'=>$guard_user , 'new_pass'=> $new_pass ));
					try {
					$ret = nahoMail::send( __('request password') ,$body , $guard_user->getProfile()->getEmail() ,array('from'=>CMS_globals::get_admin_attr('admin_mail')));
					}catch (Exception $ex){
						$body = get_partial('cms_profile/mails/req_pass' , array('guard_user'=>$guard_user , 'new_pass'=> $new_pass ));
						file_put_contents(sfConfig::get('sf_cache_dir').'/'.$guard_user->getProfile()->getEmail().'.txt',$body);
					}
				}


				$this->getUser()->setFlash('notice',__('A new password is sent to you mail successfully!'));
			}else{
				$this->getUser()->setFlash('error',__('The user name provided is not exist !'));
			}
		}

	}

	public function executeSuccessfullyAddedd(sfWebRequest $request)
	{

	}
	public function executeSuccessfullyEditProfile(sfWebRequest $request)
	{

	}
	public function executeEditProfile(sfWebRequest $request)
	{
		if(!$this->getUser()->isAuthenticated()){
			$this->redirect('cms_profile/register');
		}

		sfConfig::set('app_sfCMSPlugin_login_page_include_regsration' , false);

		append_title(__('Edit profile'));
		$this->guard_user = $this->getUser()->getGuardUser();
		$this->register_form = new registerForm($this->guard_user,array('edit_mode'=>true));
		//$this->form->validatorSchema['password'] = new sfValidatorString();


		if($request->isMethod('post')){
			$guard_user = $this->processForm($request, $this->register_form);

			if($guard_user){
				$this->redirect('cms_profile/successfullyEditProfile');
			}

			$this->setTemplate('register');
		}
		$this->setTemplate('register');

	}
	public function executeRegister(sfWebRequest $request)
	{
		if($this->getUser()->isAuthenticated()){
			$this->redirect('cms_profile/editProfile');
		}
		$this->register_form = new registerForm();
		append_title(__('Regsiter new user'));
		if($request->isMethod('post')){
			$guard_user = $this->processForm($request, $this->register_form);

			if($guard_user){
				$this->redirect('cms_profile/successfullyAddedd');
			}

			$this->setTemplate('register');
		}

	}
	public function executeActiviate(sfWebRequest $request)
	{
		//$this->form = new registerForm();
		append_title(__('Account activiation'));

		$this->forward404Unless( $link_profile_active = ProfileActiviationPeer::retrieveByPK( $request->getParameter('key')) ,'link profile activiation is not exist');
		$this->forward404Unless( $this->guard_user = $link_profile_active->getsfGuardUser(),'Guard user is not exsit' );

		$this->guard_user->setIsActive(1);
		$this->guard_user->save();
		$link_profile_active->delete();
	}

	/**
     * Enter description here...
     *
     * @param sfWebRequest $request
     * @param sfForm $form
     * @return sfGuardUser
     */
	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		$req_data  = $request->getParameter($form->getName());
		$form->bind($req_data, $request->getFiles($form->getName()));
		if ($form->isValid())
		{
			$guard_user = $form->save();


			return $guard_user;

		}
		return false;
	}
}
